package xin.tomdonkey.chat.udp;


import xin.tomdonkey.chat.udp.protocal.SP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;

/**
 * @author guolinyuan
 */
public class Server
{
    public static void main(String[] args) throws IOException
    {
        DatagramSocket socket = new DatagramSocket(5000);
        byte[] receiveBytes = new byte[SP.UDP_MAX_SIZE];
        DatagramPacket receivePacket = new DatagramPacket(receiveBytes,receiveBytes.length);
        while (true)
        {
            socket.receive(receivePacket);
            SocketAddress remoteAddress = receivePacket.getSocketAddress();

            SP receiveSP = SP.parseSP(receivePacket.getData());
            System.out.println(receiveSP.toString());
            System.out.println(new String(receiveSP.getLoad()));

            byte[] x = SP.ack(receiveSP.getId()).toBytes();
            socket.send(new DatagramPacket(x,x.length,remoteAddress));
        }
    }


}
